package com.tgy.leetcode;

/**
 *
 * https://leetcode-cn.com/problems/range-sum-query-immutable/
 *
 */

class NumArray01 {

    private int[] nums;

    public NumArray01(int[] nums) {

        this.nums = nums;
    }

    public int sumRange(int i, int j) {

        int result = 0;

        for (int k = i; k <= j ; k++) {

            result += nums[i];
        }

        return result;
    }
}

class NumArray {

    private int[] sums;


    public NumArray(int[] nums) {


        sums = new int[nums.length + 1];
        int sum = 0;

        /**
         *  sum[i] 等于 sum[0...i-1]
         */
        for (int i = 0; i < nums.length; i++) {

            sum += nums[i];
            nums[i+1] = sum;
        }

    }

    public int sumRange(int i, int j) {

        /**
         *  sum[j+1] 等于 sum[0...j]
         *  sum[i] 表示 sum[0...i-1]
         */
        return sums[j+1] - sums[i] ;
    }
}

public class _303_区域和检索_数组不可变 {
}
